<template>
  <div class="tbody">
    <header>
      <el-card class="card">
        <div class="left">
          <div class="avatar">
            <el-avatar shape="circle" :src="avaterUrl" :size="100"></el-avatar>
            <div class="welcome">欢迎您，{{ username }}</div>
          </div>
          <div class="security">
            账号安全：<i class="el-icon-mobile-phone"></i
            ><span class="title"> 绑定手机 </span> {{ phone
            }}<el-link href="/updatePhone">修改</el-link>
            <div>
              <i class="el-icon-message sz"></i>
              <span class="title"> 绑定邮件 </span>
              {{ email }}<el-link href="/updateEmail">修改</el-link>
            </div>
          </div>
        </div>
        <div class="right">
          <div class="ordertip">
            <div class="title">待付款订单</div>
            <div>
              <span class="num">{{ toPayCount }}</span
              >个
            </div>
          </div>
          <div class="line"></div>
          <div class="ordertip">
            <div class="title">待收货订单</div>
            <div>
              <span class="num">{{ toReceiveCount }}</span
              >个
            </div>
          </div>
        </div>
      </el-card>
    </header>
    <main>
      <el-card class="card">
        <div class="ordertitle">
          <h4>我最近的订单</h4>
          <el-link href="/order">查看全部订单 ></el-link>
        </div>
        <div
          style="width: 100%; background-color: rgb(240, 240, 240); height: 2px"
        ></div>
        <el-table :data="orderData" @expand-change="expand" v-if="haveData">
          <el-table-column type="expand" width="55">
            <template slot-scope="scope">
              <div>
                <span style="float: left">{{ scope.row.shippingUser }}</span
                ><span style="margin-left: 10%; margin-right: 10%">{{
                  scope.row.receiverMobile
                }}</span
                ><span>{{ scope.row.address }}</span>
              </div>
              <el-table :data="detailData">
                <el-table-column type="index"></el-table-column>
                <el-table-column prop="product" label="产品信息">
                  <template slot-scope="scope">
                    <div style="display: flex">
                      <img
                        :src="scope.row.mainImage"
                        height="60px"
                        width="60px"
                        @click="toProduct(scope.row)"
                        style="margin-right: 20px"
                      />
                      <div>
                        <div
                          @click="toProduct(scope.row)"
                          style="float: left; width: 300px"
                        >
                          {{ scope.row.productName }}
                        </div>
                        <div style="float: right">
                          <span style="margin-left: 100px; margin-right: 20px"
                            >x {{ scope.row.productCnt }}</span
                          ><span>￥{{ scope.row.productPrice }}</span>
                        </div>
                      </div>
                    </div>
                  </template>
                </el-table-column>
              </el-table>
            </template>
          </el-table-column>
          <!-- <el-table-column type="selection" width="55px"></el-table-column> -->
          <el-table-column prop="id" label="订单号"></el-table-column>
          <el-table-column prop="orderMoney" label="金额"></el-table-column>
          <el-table-column
            prop="orderStatus.name"
            label="订单状态"
          ></el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <div v-if="scope.row.orderStatus.code === 0">
                <el-button type="primary" size="mini" @click="pay(scope.row)"
                  >支付</el-button
                >
                <el-button
                  type="danger"
                  size="mini"
                  @click="cancelOrder(scope.row)"
                  >取消</el-button
                >
              </div>
              <div v-else-if="scope.row.orderStatus.code === 1">
                <el-button type="danger" size="mini">取消</el-button>
              </div>
              <div v-else-if="scope.row.orderStatus.code === 2">
                <el-button
                  type="danger"
                  size="mini"
                  @click="deleteOrder(scope.index, scope.row, orderData)"
                  >删除</el-button
                >
              </div>
              <div v-else-if="scope.row.orderStatus.code === 3">
                <el-button
                  type="primary"
                  size="mini"
                  @click="receiveOrder(scope.row)"
                  >签收</el-button
                >
                <el-button type="info" size="mini" @click="checkShip(scope.row)"
                  >查看物流</el-button
                >
                <el-button
                  type="danger"
                  size="mini"
                  @click="changeOrder(scope.row)"
                  >退货</el-button
                >
              </div>
              <div v-else-if="scope.row.orderStatus.code === 4">
                <el-button
                  type="primary"
                  size="mini"
                  @click="commentOrder(scope.row)"
                  >评价</el-button
                >
                <el-button
                  type="danger"
                  size="mini"
                  @click="deleteOrder(scope.index, scope.row, orderData)"
                  >删除</el-button
                >
              </div>
              <div v-else>
                <el-button
                  type="danger"
                  size="mini"
                  @click="deleteOrder(scope.index, scope.row, orderData)"
                  >删除</el-button
                >
              </div>
            </template>
          </el-table-column>
        </el-table>
        <div v-else>
          <img src="@/assets/images/no.png" style="margin-top: 50px" />
          <div>
            您最近没有购买任何商品，先去<el-link
              style="color: salmon; margin-top: -5px; font-size: 16px"
              href="/"
            >
              挑选商品 </el-link
            >吧~
          </div>
        </div>
      </el-card>
    </main>
    <el-dialog :title="title" :visible="visible" :before-close="handleClose">
      <div v-if="title === '支付'">
        <div>
          请在<span style="color: pink; font-size: 20px; margin: 5px"
            >20分钟</span
          >内支付订单，否则订单将自动取消。
        </div>
      </div>
      <div v-else>
        <Ship></Ship>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import ShareData from "@/utils/localStorage.js";
import { selectById } from "@/api/member.js";
import Ship from "@/views/member/order/Ship.vue"
import {
  selectByMemberId,
  selectDetailsByOrderId,
  updateOrder,
} from "@/api/order.js";
import { selectByIdA } from "@/api/address.js";
export default {
  components:{Ship},
  data() {
    return {
      title:"支付",
      visible:false,
      username: "admin",
      phone: "",
      email: "",
      avaterUrl: "",
      haveData: false,
      orderData: [],
      toPayCount: 0,
      toReceiveCount: 0,
      keyword: { pageSize: 10, currentPage: 1 },
      detailData: [],
      head: "https://zpl-1303223223.cos.ap-beijing.myqcloud.com",
    };
  },
  methods: {
    getMemberInfo() {
      let memberId = ShareData.getItem("memberId");
      selectById(memberId).then((res) => {
        console.log(res);
        if (res.data.code === "0000") {
          this.username = res.data.data.nickname;
          this.phone = res.data.data.phone;
          this.email = res.data.data.email;
          this.avaterUrl = this.head + res.data.data.headPortrait;
        }
      });
      this.toPayCount = 0;
      this.toReceiveCount = 0;
      this.keyword = { memberId: memberId, pageSize: 10, currentPage: 1 };
      selectByMemberId(this.keyword).then((res) => {
        console.log(res);
        if (res.data.code === "0000") {
          if (res.data.data.records.length === 0) {
            this.toPayCount = 0;
            this.toReceiveCount = 0;
            this.haveData = false;
          } else {
            this.haveData = true;
            for (let i = 0; i < res.data.data.records.length; i++) {
              if (res.data.data.records[i].orderStatus.code === 0) {
                this.toPayCount++;
              } else if (res.data.data.records[i].orderStatus.code === 3) {
                this.toReceiveCount++;
              }
              this.keyword.day = 90;
              this.keyword.orderStatus = null;
              selectByMemberId(this.keyword).then((res) => {
                if (res.data.code === "0000") {
                  if (res.data.data.records.length !== 0) {
                    this.haveData = true;
                    this.orderData = res.data.data.records;
                  } else {
                    this.haveData = false;
                  }
                }
              });
            }
          }
        }
      });
    },
    expand(row) {
      selectByIdA(row.addressId).then((res) => {
        console.log(res);
        if (res.data.code === "0000") {
          row.receiverMobile = res.data.data.receiverMobile;
          row.address =
            res.data.data.receiverProvince +
            res.data.data.receiverCity +
            res.data.data.receiverDistrict +
            res.data.data.receiverZip;
        }
      });
      selectDetailsByOrderId(row.id).then((res) => {
        console.log(res);
        if (res.data.code === "0000") {
          this.detailData = res.data.data;
          for (let i = 0; i < res.data.data.length; i++) {
            this.detailData[i].mainImage =
              this.head + res.data.data[i].mainImage;
          }
        }
      });
    },
    pay(row) {
      this.visible = true;
      this.title = "支付";
      console.log(row);
    },
    cancelOrder(row) {
      if (confirm("确定要取消订单吗？")) {
        this.order = row;
        this.order.paymentMethod = row.paymentMethod.code;
        this.order.orderStatus = 2;
        updateOrder(this.order).then((res) => {
          if (res.data.code === "0000") {
            // this.$message.success("取消成功")
            this.showOrder();
          }
        });
      }
    },
    deleteOrder(index, row, orderData) {
      if (confirm("确定要删除订单吗？")) {
        this.order = row;
        this.order.orderStatus = row.orderStatus.code;
        this.order.paymentMethod = row.paymentMethod.code;
        this.order.deleteStatus = 1;
        this.total--;
        updateOrder(this.order).then((res) => {
          if (res.data.code === "0000") {
            orderData.splice(index, 1);
          }
        });
      }
    },
    receiveOrder(row) {
      if (confirm("确定要签收订单吗？")) {
        this.order = row;
        this.order.paymentMethod = row.paymentMethod.code;
        this.order.orderStatus = 4;
        updateOrder(this.order).then((res) => {
          if (res.data.code === "0000") {
            this.showOrder();
          }
        });
      }
    },
    checkShip() {
      this.title = "物流信息";
      this.visible = true;
    },
    changeOrder(row) {
      if (confirm("确定要退货吗？")) {
        this.order = row;
        this.order.paymentMethod = row.paymentMethod.code;
        this.order.orderStatus = 5;
        updateOrder(this.order).then((res) => {
          if (res.data.code === "0000") {
            this.showOrder();
          }
        });
      }
    },
    handleClose() {
      this.visible = false;
    },
    commentOrder() {
      this.$message.success("本服务暂未开放");
    },
  },
  mounted() {
    this.getMemberInfo();
  },
};
</script>

<style lang="scss" scoped>
.tbody {
  width: 100%;
}
.avatar {
  line-height: -60px;
  display: flex;
}
.left {
  float: left;
  text-align: left;
  margin-left: 30px;
}
.right {
  float: right;
  display: flex;
  background-color: rgb(240, 240, 240);
  padding: 3%;
  margin-right: 30px;
}
.card {
  padding: 3%;
  margin-bottom: 1%;
}
.line {
  height: 60px;
  border: 1px solid #bbb;
  background-color: #bbb;
  margin-left: 30px;
  margin-right: 30px;
}
.ordertip .title {
  margin-bottom: 10px;
}
.ordertip .num {
  margin: 10px;
  font-size: 20px;
  color: salmon;
}
.welcome {
  margin-top: 40px;
  margin-bottom: 10px;
}
.security {
  margin-top: 5px;
  margin-bottom: 5px;
  line-height: 30px;
}
.security .title {
  color: silver;
}
.security .sz {
  margin-left: 80px;
}
.security .el-link {
  margin-left: 50px;
}
.ordertitle {
  text-align: left;
  margin-top: -20px;
}
.ordertitle .el-link {
  margin-top: -30px;
  float: right;
  color: silver;
}
</style>